﻿//-----------------------------------------------------------------------
// <copyright file="OperationResult.cs" company="AFS">
// Copyright (c) AFS. All rights reserved.
//
//    This file is part of Chatbox.
//
//    Chatbox is free software: you can redistribute it and/or modify
//    it under the terms of the GNU Lesser General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    any later version.
//
//    Chatbox is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//    GNU Lesser General Public License for more details.
//
//    You should have received a copy of the GNU Lesser General Public License
//    along with Chatbox. If not, see http://www.gnu.org/copyleft/lesser.html
// </copyright>
//----------------------------------------------------------------------- 

namespace ChatBox.DomainObjects.ResultData
{
    using System;

    /// <summary>
    /// delegate type for an operation result
    /// </summary>
    /// <param name="data">The data received</param>
    public delegate void OperationResultHandler(OperationResult data);

    /// <summary>
    /// Result of a threaded operation
    /// Can be used as is or as a base class
    /// </summary>
    public class OperationResult
    {
        /// <summary>
        /// Initializes a new instance of the OperationResult class with no params
        /// </summary>
        public OperationResult()
        {
            this.OperationTime = DateTime.Now;
        }

        /// <summary>
        /// Initializes a new instance of the OperationResult class with params
        /// </summary>
        /// <param name="success">the success flag</param>
        /// <param name="message">an error or sucess message</param>
        /// <param name="error">the exception</param>
        public OperationResult(bool success, string message, Exception error)
        {
            this.OperationTime = DateTime.Now;
            this.Success = success;
            this.Message = message;
            this.Error = error;
        }

        /// <summary>
        /// Initializes a new instance of the OperationResult class with params
        /// </summary>
        /// <param name="success">the success flag</param>
        /// <param name="message">an error or sucess message</param>
        public OperationResult(bool success, string message)
        {
            this.OperationTime = DateTime.Now;
            this.Success = success;
            this.Message = message;
        }

        /// <summary>
        /// Gets or sets a value indicating whether the operation suceeded
        /// </summary>
        public bool Success { get; protected set; }

        /// <summary>
        /// Gets or sets the error exception 
        /// </summary>
        public Exception Error { get; protected set; }

        /// <summary>
        /// Gets or sets the success or error message
        /// </summary>
        public string Message { get; set; }

        /// <summary>
        /// Gets or sets this result's sequence. Used only on success messages
        /// </summary>
        public int Sequence { get; set; }

        /// <summary>
        /// Gets or sets the time at which the operation occured
        /// </summary>
        public DateTime OperationTime { get; protected set; }

        /// <summary>
        /// Gets the message or failing that the error message
        /// </summary>
        public string MessageOrErrorMessage
        {
            get
            {
                string messageOrErrorMessage = this.Message;
                if (string.IsNullOrEmpty(messageOrErrorMessage) && (this.Error != null))
                {
                    messageOrErrorMessage = this.Error.Message;
                }

                return messageOrErrorMessage; 
            }
        }
    }
}
